package com.jiliang.common.quartz;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;

/**
 * aop 执行Quartz
 *
 * @author
 * @version v_1.0.1
 * @since 2018/12/1 16:18
 */
@Aspect
@Component
public class QuartzExecuteAop {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

//    @Resource
//    RedisService redisService;

    @Pointcut("@annotation(QuartzAnnotation)")
    public void quartzAspect() {
    }

    @Around("quartzAspect()")
    public Object quartzAop(ProceedingJoinPoint point) throws Throwable {
        logger.info("进入自动任务切面方法");
        //获取目标类名
        String simpleName = point.getTarget().getClass().getSimpleName();
        //获取目标方法签名
        String signature = point.getSignature().toString();
        String methodName = signature.substring(signature.lastIndexOf(".") + 1, signature.indexOf("("));
        Method sourceMethod = getSourceMethod(point);
//        String ip = IpDsUtil.getIp();
//        logger.info("自动任务切面ip【"+ ip +"】");
//        com.nxin.etposvr.jiliang.common.aop.QuartzAnnotation annotation = sourceMethod.getAnnotation(com.nxin.etposvr.jiliang.common.aop.QuartzAnnotation.class);
//        long expireTime = annotation.expireTime();
//        TimeUnit timeUnit = annotation.timeUnit();
//        String redisKey = "quartz:" + simpleName + ":" + methodName;
//        logger.info("执行自动任务redisKey【" + redisKey + "】");
//        RedisLock rLock = redisService.getRLock(redisKey + "lock");
//        rLock.lock();
//        Object o = redisService.get(redisKey);
//        logger.info("自动任务切面redisValue【"+ o +"】");
//        if (o != null) {
//            logger.info("结束自动任务切面方法【正在执行】");
//            logger.info("服务器[" + ip + "]正在执行[" + redisKey + "]");
//            return null;
//        }
//        redisService.set(redisKey, "服务器" + ip, expireTime, timeUnit);
        Object proceed = point.proceed();
        logger.info("结束自动任务切面方法【正常】");
//        rLock.unlock();
        return proceed;
    }

    private HttpServletRequest getRequet(ProceedingJoinPoint pjp) {
        Object target=pjp.getTarget();
        HttpServletRequest retValue;
        try {
            Method reqMethod=target.getClass().getMethod("getRequest");
            retValue=(HttpServletRequest) reqMethod.invoke(target);
        } catch (Exception e) {
            return null;
        }
        return retValue;
    }

    /**
     * @param
     * @explain 获得注解
     * @author
     * @version v_1.0.1
     * @since 22017/10/17 11:14
     */
    private static Method getSourceMethod(ProceedingJoinPoint jp) {
        Method proxyMethod = ((MethodSignature) jp.getSignature()).getMethod();
        try {
            return jp.getTarget().getClass().getMethod(proxyMethod.getName(), proxyMethod.getParameterTypes());
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        }
        return null;
    }
}